package
{
	import flash.filters.GlowFilter;
	import flash.utils.Dictionary;
	
	import org.flixel.*;

	public class LootStateLevelImage extends FlxGroup
	{
		protected var button:FlxSprite;
		protected var buttonText:FlxText;
		protected var hoverFilter:GlowFilter;
		
		protected var unlocked:Boolean;
		protected var level:int;
		protected var overlapped:Boolean;
		
		public function LootStateLevelImage(X:int, Y:int, l:int, state:int)
		{
			level = l;
			unlocked = true;
			overlapped = false;
			
			button = new FlxSprite(X, Y);
			var buttonBg:Class;
			var color:uint;
			
			if (state == 3) {
				button.loadGraphic(Res.locked);
				unlocked = false;
				add(button);
			} else {
				if (level >= 0 && level < 15) {
					buttonBg = Res.grassButton;
					color = 0xFFFFFF;
				} else if (level >= 15 && level < 30) {
					buttonBg = Res.barrenButton;
					color = 0xFFFFFF;
				} else {
					buttonBg = Res.voidButton;
					color = 0xE06D1B;
				}
				button.loadGraphic(buttonBg);
				
				var text:String = (state == 1) ? String(level + 1) : "?";
				buttonText = new FlxText(X, Y, button.width, text, false);
				buttonText.setFormat("Courier New", 24, color, "center", 10, true);
				buttonText.y = (button.height - buttonText.height)/2 + button.y;
				
				add(button);
				add(buttonText);
				
				hoverFilter = new GlowFilter(0xFFFFFF, 0.8, 10, 10, 3);
			} 
		}
		
		public override function update():void {
			super.update();
			
			if (button.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!overlapped) {
					button.setFilter(hoverFilter);
					overlapped = true;
				}
			} else {
				if (overlapped) {
					button.removeFilter();
					overlapped = false;
				}
			}
			
			if (FlxG.mouse.justPressed() && overlapped && unlocked) {
				FlxG.switchState(new GameState(level + 1));
			}
		}
		
		public function getLevel():int {
			if (!unlocked) return -1;
			return level + 1;
		}	
	}
}